using System.Web.UI.WebControls;
using System;
using StillVoiceCommon;
using TeamTime.Classes;
namespace TeamTime
{
    partial class UsersGroups : System.Web.UI.Page
    {
        //StillVoiceCommon.Functions f = new StillVoiceCommon.Functions();
        Security sec;
        #region " Web Form Designer Generated Code "

        //This call is required by the Web Form Designer. 
        [System.Diagnostics.DebuggerStepThrough()]
        private void InitializeComponent()
        {

        }


        private void Page_Init(object sender, System.EventArgs e)
        {
            //CODEGEN: This method call is required by the Web Form Designer 
            //Do not modify it using the code editor. 
            InitializeComponent();
        }

        #endregion

        private void Page_Load(object sender, System.EventArgs e)
        {
            sec = new Security(sqlDB.getDBConnString());
            //Put user code to initialize the page here 
            if ((Request.QueryString["ID"] != null))
            {
                txtUserID.Text = Request.QueryString["id"];

                this.lblUserName.Text = sec.getUserName(Convert.ToInt32(this.txtUserID.Text));
                if (!Page.IsPostBack)
                {
                    buildListboxes();
                }
            }
            enableControls();
        }
        private void enableControls()
        {
            if (!sec.CanDo(Convert.ToInt32(Session["UserID"]), "Usersedit"))
            {
                Functions.disableChildControls(this.Page);
            }
        }

        protected void btnRemove_Click(object sender, EventArgs e) 
        { 
            User u = new User(Convert.ToInt32(Session["UserID"])); 
            u.load(this.txtUserID.Text, "ID",false); 
            ;
            foreach (ListItem item in this.lstYes.Items)
            { 
                if (item.Selected) {
                    if (Convert.ToInt32(u.Fields["DefaultUserGroupID"]) == Convert.ToInt32(item.Value))
                        this.LabelWithTimedMsg1.ShowStatusMessage(this.lblUserName.Text + " cannot be removed from the group " + item.Text);
                    else
                        u.removeFromGroup(Convert.ToInt64(item.Value)); 
                } 
            } 
            u = null; 
            buildListboxes(); 
        }
        protected void btnAdd_Click(object sender, EventArgs e) 
        {
            StillVoiceCommon.User u = new StillVoiceCommon.User(Convert.ToInt32(Session["UserID"])); 
            u.load(this.txtUserID.Text, "ID",false); 
            
            foreach (ListItem item in this.lstNo.Items)
            { 
                if (item.Selected) {
                    u.addToGroup(Convert.ToInt64(item.Value)); 
                } 
            } 
            u = null; 
            buildListboxes(); 
        }
        private void buildListboxes()
        {
            string sql;
            sql = "select g.ID, g.UserGroup from Security.[Group] g " + "inner join Security.GroupUser gu on g.ID = gu.UserGroupID " + "where gu.UserID = " + this.txtUserID.Text + " order by g.UserGroup ";
            Functions.listBoxPopulate(this.lstYes, sql, "ID", "UserGroup");

            sql = "select g.ID, g.UserGroup from Security.[Group] g " + "left join Security.GroupUser gu on g.ID = gu.UserGroupID " + " and gu.UserID = " + this.txtUserID.Text + " where gu.UserID is null " + " order by g.UserGroup ";
            Functions.listBoxPopulate(this.lstNo, sql, "ID", "UserGroup");
        }

    }

}